/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.pms.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.expert.domain.Expert;
import com.fzhucc.modules.expert.repository.ExpertRepository;
import com.fzhucc.modules.pms.domain.Project;
import com.fzhucc.modules.pms.domain.ProjectScore;
import com.fzhucc.modules.pms.domain.dto.*;
import com.fzhucc.modules.pms.repository.ProjectRepository;
import com.fzhucc.modules.pms.repository.ProjectScoreRepository;
import com.fzhucc.modules.system.domain.User;
import com.fzhucc.modules.system.repository.UserRepository;
import com.fzhucc.modules.thinktank.domain.Academician;
import com.fzhucc.modules.thinktank.domain.Capital;
import com.fzhucc.modules.thinktank.domain.Company;
import com.fzhucc.modules.thinktank.domain.Leader;
import com.fzhucc.modules.thinktank.repository.AcademicianRepository;
import com.fzhucc.modules.thinktank.repository.CapitalRepository;
import com.fzhucc.modules.thinktank.repository.CompanyRepository;
import com.fzhucc.modules.thinktank.repository.LeaderRepository;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2022-11-23
 **/
@Service
@RequiredArgsConstructor
public class ProjectScoreService {

    private final ProjectScoreRepository projectScoreRepository;

    private final ExpertRepository expertRepository;

    private final ProjectRepository projectRepository;

    private final ProjectService projectService;

    private final AcademicianRepository academicianRepository;

    private final CapitalRepository capitalRepository;

    private final LeaderRepository leaderRepository;

    private final CompanyRepository companyRepository;

    private final UserRepository userRepository;

    public Map<String, Object> getProjectByLoginUser(Pageable pageable) {
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        Page<Project> page = projectRepository.findAllByExpectUser(userId, pageable);
        return PageUtil.toPage(page);
    }

    public List<Expert> getExpertByProjectId(String projectId) {
        List<Expert> expert_list = new ArrayList<Expert>();
        List<ProjectScore> list = projectScoreRepository.findByProjectId(projectId);
        if (list == null) {
            throw new BadRequestException("项目评分表项目未找到");
        }
        for (int i = 0; i < list.size(); i++) {
            ProjectScore projectScore = list.get(i);
            Expert expert = projectScore.getExpert();
            if (expert != null) {
                expert_list.add(expert);
            }
        }
        return expert_list;
    }

    //去重
    public static List removeDuplicate(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    public List<Integer> getDifferenceSet(List<Integer> allList, List<Integer> list) {
        List<Integer> resultList = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        list.forEach(i2 -> {
            map.put(i2 + "", i2);
        });
        allList.forEach(i1 -> {
            Integer m = map.get(i1 + "");
            //如果为空，则证明list2中无该数据
            if (m == null) {
                resultList.add(i1);
            }
        });
        return resultList;
    }

    /*@Transactional*/
    public void dodistribution(ProjectScoreDistributionDto projectScoreDistributionDto) {
        String project_id = projectScoreDistributionDto.getProjectId();
        List<Integer> list = projectScoreDistributionDto.getListExpertId();
        List<Integer> repeatlist = new ArrayList<Integer>();
        Optional<Project> optionalProject = projectRepository.findById(project_id);
        if (optionalProject.isEmpty()) {
            throw new BadRequestException("项目未找到");
        }
        if (list == null) {
            throw new BadRequestException("专家id list未找到");
        }
        // 专家list 和原来的专家list比较   和原来一样的则不删除 评分表  没有的新增   有删除的则删除
        List<ProjectScore> list_projectScore = projectScoreRepository.findByProjectId(project_id);
        if (list_projectScore != null && list_projectScore.size() > 0) {
            for (int j = 0; j < list_projectScore.size(); j++) {
                ProjectScore projectScore = list_projectScore.get(j);
                if (projectScore != null) {
                    Integer expert_id = projectScore.getExpert().getId();
                    //未打过分 projectScore.getScore()==null
                    if (list.indexOf(expert_id) == -1) { //不在则删除
                        projectScoreRepository.deleteById(projectScore.getId());
                    } else {
                        repeatlist.add(expert_id);
                    }
                }
            }
            list = getDifferenceSet(list, repeatlist);
        }
        for (int i = 0; i < list.size(); i++) {
            Integer expectId = list.get(i);
            List<ProjectScore> list1 = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expectId, project_id, optionalProject.get().getLevel());
            if (list1 == null || list1.size() == 0) {
                ProjectScore projectScore = new ProjectScore();
                Optional<Expert> res = expertRepository.findById(expectId);
                if (!res.isPresent()) {
                    throw new BadRequestException("专家未找到");
                }
                Expert expert = res.get();
                if (expert == null) {
                    throw new BadRequestException("专家未找到");
                }
                projectScore.setExpert(expert);

                Optional<Project> res1 = projectRepository.findById(project_id);
                if (!res1.isPresent()) {
                    throw new BadRequestException("项目未找到");
                }
                Project project = res1.get();
                if (project == null) {
                    throw new BadRequestException("项目未找到");
                }
                projectScore.setProject(project);
                projectScore.setProjectLevel(project.getLevel());
                projectScoreRepository.save(projectScore);
            }
        }
    }

    //随机专家
    //expert_counts_list 原查询出来的所有的专家Id list
    //counts 用户需要的数量
    public List<Integer> randomAllExpert(List<Integer> expert_counts_list, int counts) {
        List<Integer> expert_result = new ArrayList<Integer>();//最后输出的不重复的随机的id
        Random rand = new Random();
        for (int i = 0; i < counts; i++) {
            int randomIndex = rand.nextInt(expert_counts_list.size());
            Integer value = expert_counts_list.get(randomIndex);
            expert_counts_list.remove(randomIndex);
            expert_result.add(value);
        }
        return expert_result;
    }

    //随机院士之家
    //academician_counts_list 原查询出来的所有的院士之家Id list
    //counts 用户需要的数量
    public List<Integer> randomAllAcademician(List<Integer> academician_counts_list, int counts) {
        List<Integer> academician_result = new ArrayList<Integer>();//最后输出的不重复的随机的id
        Random rand = new Random();
        for (int i = 0; i < counts; i++) {
            int randomIndex = rand.nextInt(academician_counts_list.size());
            Integer value = academician_counts_list.get(randomIndex);
            academician_counts_list.remove(randomIndex);
            academician_result.add(value);
        }
        return academician_result;
    }

    //随机产业资本
    //capital_counts_list 原查询出来的所有的产业资本Id list
    //counts 用户需要的数量
    public List<Integer> randomAllCapital(List<Integer> capital_counts_list, int counts) {
        List<Integer> capital_result = new ArrayList<Integer>();//最后输出的不重复的随机的id
        Random rand = new Random();
        for (int i = 0; i < counts; i++) {
            int randomIndex = rand.nextInt(capital_counts_list.size());
            Integer value = capital_counts_list.get(randomIndex);
            capital_counts_list.remove(randomIndex);
            capital_result.add(value);
        }
        return capital_result;
    }

    //随机产业龙头
    //leader_counts_list 原查询出来的所有的产业龙头Id list
    //counts 用户需要的数量
    public List<Integer> randomAllLeader(List<Integer> leader_counts_list, int counts) {
        List<Integer> leader_result = new ArrayList<Integer>();//最后输出的不重复的随机的id
        Random rand = new Random();
        for (int i = 0; i < counts; i++) {
            int randomIndex = rand.nextInt(leader_counts_list.size());
            Integer value = leader_counts_list.get(randomIndex);
            leader_counts_list.remove(randomIndex);
            leader_result.add(value);
        }
        return leader_result;
    }

    //随机上市公司
    //expert_counts_list 原查询出来的所有的上市公司Id list
    //counts 用户需要的数量
    public List<Integer> randomAllCompany(List<Integer> company_counts_list, int counts) {
        List<Integer> company_result = new ArrayList<Integer>();//最后输出的不重复的随机的id
        Random rand = new Random();
        for (int i = 0; i < counts; i++) {
            int randomIndex = rand.nextInt(company_counts_list.size());
            Integer value = company_counts_list.get(randomIndex);
            company_counts_list.remove(randomIndex);
            company_result.add(value);
        }
        return company_result;
    }

    //随机专家
    public List<ProjectExpertRandomRespDto> expertRandom(List<String> manType, int counts, String projectId) {
        Optional<Project> res = projectRepository.findById(projectId);
        if (!res.isPresent()) {
            throw new BadRequestException("项目未找到");
        }
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<Expert> list_expert = new ArrayList<Expert>();
        if (manType == null || manType.size() == 0) {
            list_expert = expertRepository.findExpertWithprojectId(projectId); //去除已分配的专家
        } else {
            if (manType.size() > 0) {
                for (String a : manType) {
                    List<Expert> listRes = expertRepository.findExpertByTypeWithprojectId(a, projectId);//去除已分配的专家
                    if (listRes != null && listRes.size() > 0) {
                        for (Expert expert : listRes) {
                            list_expert.add(expert);
                        }
                    }
                }
            } else {
                throw new BadRequestException("专家库类型数组小于零无法操作");
            }
        }
        List<Integer> expert_counts_list = new ArrayList<Integer>();
        if (list_expert != null) {
            //判断数量<随机人数则提示 数据库内该种分类专家数量
            if (list_expert.size() < counts) {
                throw new BadRequestException("专家库人数少于随机人数,专家库总人数为:" + list_expert.size() + ",请重新选择随机人数数量");
            }
            for (Expert expert : list_expert) {
                expert_counts_list.add(expert.getId());
            }
        }
        List<Integer> expert_result = randomAllExpert(expert_counts_list, counts);
        for (Integer id : expert_result) {
            Optional<Expert> res2 = expertRepository.findById(id);
            if (res2.isPresent()) {
                ProjectExpertRandomRespDto projectExpertRandomRespDto = new ProjectExpertRandomRespDto();
                projectExpertRandomRespDto.setId(res2.get().getId());
                projectExpertRandomRespDto.setName(res2.get().getRealname());
                projectExpertRandomRespDto.setPhone(res2.get().getPhone());
                list.add(projectExpertRandomRespDto);
            }
        }
        return list;
    }


    //随机院士之家
    public List<ProjectExpertRandomRespDto> academicianRandom(List<String> manType, int counts, String projectId) {
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<Academician> list_academician = new ArrayList<Academician>();
        if (manType == null || manType.size() == 0) {
            list_academician = academicianRepository.findByProjectId(projectId);
        } else {
            if (manType.size() > 0) {
                for (String a : manType) {
                    List<Academician> listRes = academicianRepository.findByTypeProjectId(a, projectId);//去除已分配的院士之家
                    if (listRes != null && listRes.size() > 0) {
                        for (Academician expert : listRes) {
                            list_academician.add(expert);
                        }
                    }
                }
            } else {
                throw new BadRequestException("院士之家类型数组小于零无法操作");
            }
        }
        List<Integer> academician_counts_list = new ArrayList<Integer>();
        if (list_academician != null) {
            //判断数量<随机人数则提示 数据库内该种分类专家数量
            if (list_academician.size() < counts) {
                throw new BadRequestException("院士之家专家库人数少于随机人数,院士之家专家库总人数为:" + list_academician.size() + ",请重新选择随机人数数量");
            }
            for (Academician academician : list_academician) {
                academician_counts_list.add(academician.getId().intValue());
            }
        }
        List<Integer> academician_result = randomAllAcademician(academician_counts_list, counts);
        for (Integer id : academician_result) {
            Optional<Academician> res = academicianRepository.findById(Long.valueOf(id));
            if (res.isPresent()) {
                ProjectExpertRandomRespDto projectExpertRandomRespDto = new ProjectExpertRandomRespDto();
                projectExpertRandomRespDto.setId(res.get().getId().intValue());
                projectExpertRandomRespDto.setName(res.get().getName());
                if (res.get().getUserId() != null) {
                    Optional<User> userRes = userRepository.findById(res.get().getUserId());
                    if (userRes.isPresent()) {
                        projectExpertRandomRespDto.setPhone(userRes.get().getPhone());
                    }
                }
                list.add(projectExpertRandomRespDto);
            }
        }
        return list;
    }


    //随机产业资本
    public List<ProjectExpertRandomRespDto> capitalRandom(List<String> manType, int counts, String projectId) {
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<Capital> list_capital = new ArrayList<Capital>();
        if (manType == null || manType.size() == 0) {
            list_capital = capitalRepository.findByProjectId(projectId);
        } else {
            if (manType.size() > 0) {
                for (String a : manType) {
                    List<Capital> listRes = capitalRepository.findByTypeProjectId(a, projectId);//去除已分配的产业资本
                    if (listRes != null && listRes.size() > 0) {
                        for (Capital expert : listRes) {
                            list_capital.add(expert);
                        }
                    }
                }
            } else {
                throw new BadRequestException("产业资本类型数组小于零无法操作");
            }
        }
        List<Integer> capital_counts_list = new ArrayList<Integer>();
        if (list_capital != null) {
            //判断数量<随机人数则提示 数据库内该种分类专家数量
            if (list_capital.size() < counts) {
                throw new BadRequestException("产业资本专家库人数少于随机人数,产业资本专家库总人数为:" + list_capital.size() + ",请重新选择随机人数数量");
            }
            for (Capital capital : list_capital) {
                capital_counts_list.add(capital.getId().intValue());
            }
        }
        List<Integer> capital_result = randomAllCapital(capital_counts_list, counts);
        for (Integer id : capital_result) {
            Optional<Capital> res = capitalRepository.findById(Long.valueOf(id));
            if (res.isPresent()) {
                ProjectExpertRandomRespDto projectExpertRandomRespDto = new ProjectExpertRandomRespDto();
                projectExpertRandomRespDto.setId(res.get().getId().intValue());
                projectExpertRandomRespDto.setName(res.get().getName());
                if (res.get().getUserId() != null) {
                    Optional<User> userRes = userRepository.findById(res.get().getUserId());
                    if (userRes.isPresent()) {
                        projectExpertRandomRespDto.setPhone(userRes.get().getPhone());
                    }
                }
                list.add(projectExpertRandomRespDto);
            }
        }
        return list;
    }


    //随机产业龙头
    public List<ProjectExpertRandomRespDto> leaderRandom(List<String> manType, int counts, String projectId) {
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<Leader> list_leader = new ArrayList<Leader>();
        if (manType == null || manType.size() == 0) {
            list_leader = leaderRepository.findByProjectId(projectId);
        } else {
            if (manType.size() > 0) {
                for (String a : manType) {
                    List<Leader> listRes = leaderRepository.findByTypeProjectId(a, projectId);//去除已分配的产业龙头
                    if (listRes != null && listRes.size() > 0) {
                        for (Leader expert : listRes) {
                            list_leader.add(expert);
                        }
                    }
                }
            } else {
                throw new BadRequestException("产业龙头类型数组小于零无法操作");
            }
        }
        List<Integer> leader_counts_list = new ArrayList<Integer>();
        if (list_leader != null) {
            //判断数量<随机人数则提示 数据库内该种分类专家数量
            if (list_leader.size() < counts) {
                throw new BadRequestException("产业龙头专家库人数少于随机人数,产业龙头专家库总人数为:" + list_leader.size() + ",请重新选择随机人数数量");
            }
            for (Leader leader : list_leader) {
                leader_counts_list.add(leader.getId().intValue());
            }
        }
        List<Integer> leader_result = randomAllLeader(leader_counts_list, counts);
        for (Integer id : leader_result) {
            Optional<Leader> res = leaderRepository.findById(Long.valueOf(id));
            if (res.isPresent()) {
                ProjectExpertRandomRespDto projectExpertRandomRespDto = new ProjectExpertRandomRespDto();
                projectExpertRandomRespDto.setId(res.get().getId().intValue());
                projectExpertRandomRespDto.setName(res.get().getName());
                if (res.get().getUserId() != null) {
                    Optional<User> userRes = userRepository.findById(res.get().getUserId());
                    if (userRes.isPresent()) {
                        projectExpertRandomRespDto.setPhone(userRes.get().getPhone());
                    }
                }
                list.add(projectExpertRandomRespDto);
            }
        }
        return list;
    }

    //随机上市公司
    public List<ProjectExpertRandomRespDto> companyRandom(List<String> manType, int counts, String projectId) {
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<Company> list_company = new ArrayList<Company>();
        if (manType == null || manType.equals("")) {
            list_company = companyRepository.findByProjectId(projectId);
        } else {
            if (manType.size() > 0) {
                for (String a : manType) {
                    List<Company> listRes = companyRepository.findByTypeProjectId(a, projectId);//去除已分配的上市公司
                    if (listRes != null && listRes.size() > 0) {
                        for (Company expert : listRes) {
                            list_company.add(expert);
                        }
                    }
                }
            } else {
                throw new BadRequestException("上市公司类型数组小于零无法操作");
            }
        }
        List<Integer> company_counts_list = new ArrayList<Integer>();
        if (list_company != null) {
            //判断数量<随机人数则提示 数据库内该种分类专家数量
            if (list_company.size() < counts) {
                throw new BadRequestException("上市公司专家库人数少于随机人数,上市公司专家库总人数为:" + list_company.size() + ",请重新选择随机人数数量");
            }
            for (Company company : list_company) {
                company_counts_list.add(company.getId().intValue());
            }
        }
        List<Integer> company_result = randomAllCompany(company_counts_list, counts);
        for (Integer id : company_result) {
            Optional<Company> res = companyRepository.findById(Long.valueOf(id));
            if (res.isPresent()) {
                ProjectExpertRandomRespDto projectExpertRandomRespDto = new ProjectExpertRandomRespDto();
                projectExpertRandomRespDto.setId(res.get().getId().intValue());
                projectExpertRandomRespDto.setName(res.get().getName());
                if (res.get().getUserId() != null) {
                    Optional<User> userRes = userRepository.findById(res.get().getUserId());
                    if (userRes.isPresent()) {
                        projectExpertRandomRespDto.setPhone(userRes.get().getPhone());
                    }
                }
                list.add(projectExpertRandomRespDto);
            }
        }
        return list;
    }

    @Transactional
    public List<ProjectExpertRandomRespDto> randomManList(ProjectRandomManTypeDto projectRandomManTypeDto) {
        List<ProjectExpertRandomRespDto> list = new ArrayList<ProjectExpertRandomRespDto>();
        List<String> listManType = projectRandomManTypeDto.getManType(); //专家类型list
        String manIdentity = projectRandomManTypeDto.getManIdentity();
        int counts = projectRandomManTypeDto.getCounts();
        String projectId = projectRandomManTypeDto.getProjectId();
        /*if (manType == null || manType.equals("")) {
            throw new BadRequestException("输入正确类型");
        }*/
        if (manIdentity == null || manIdentity.equals("")) {
            throw new BadRequestException("输入正确身份");
        }
        if (counts < 0 || counts == 0) {
            throw new BadRequestException("输入正确随机人数");
        }

        //TODO 随机专家根据登录用户的orgArea进行随机 待完善
        if (manIdentity.equals("expert")) {//专家
            list = expertRandom(listManType, counts, projectId);
        } else if (manIdentity.equals("academician")) {//院士之家
            list = academicianRandom(listManType, counts, projectId);
        } else if (manIdentity.equals("capital")) {//产业资本
            list = capitalRandom(listManType, counts, projectId);
        } else if (manIdentity.equals("leader")) {//产业龙头
            list = leaderRandom(listManType, counts, projectId);
        } else if (manIdentity.equals("company")) {//上市公司
            list = companyRandom(listManType, counts, projectId);
        } else {
            throw new BadRequestException("未找到分类类型");
        }
        return list;
    }


    public void expertRating(ProjectExpertScoreDto projectExpertScoreDto) {
        BigDecimal score = projectExpertScoreDto.getScore();
        String projectId = projectExpertScoreDto.getProjectId();
        if (score == null) {
            throw new BadRequestException("评分为null");
        }
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("用户ID未找到");
        }
        Optional<Project> optionalProject = projectRepository.findById(projectId);
        if (optionalProject.isEmpty()) {
            throw new BadRequestException("项目未找到");
        }
        Expert expert = expertRepository.findByUserId(userId);
        if (expert == null) {
            throw new BadRequestException("未找到专家");
        }
        Integer expertId = expert.getId();
        //专家评分只能查找当前阶段level进行评分
        List<ProjectScore> list = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expertId, projectId, optionalProject.get().getLevel());
        if (list == null || list.size() != 1) {
            throw new BadRequestException("专家未分配指定项目");
        }
        ProjectScore projectScore = list.get(0);
        projectScore.setScore(score);
        projectScore.setScoreTime(new Date());
        projectScore.setAdvice(projectExpertScoreDto.getAdvice());
        projectScoreRepository.save(projectScore);
    }

    public ProjectDto getProjectScoreByProjectId(String projectId) {
        ProjectDto projectDto = projectService.findById(projectId);
        if (projectDto == null) {
            throw new BadRequestException("项目未找到");
        }
        List<ProjectScore> list = projectScoreRepository.findByProjectId(projectId);
        projectDto.setExpertScoreSituation(list);
        return projectDto;
    }

    public ProjectDto getProjectScoreByProjectIdSiftLevel(String projectId) {
        ProjectDto projectDto = projectService.findById(projectId);
        if (projectDto == null) {
            throw new BadRequestException("项目未找到");
        }
        List<ProjectScore> list1 = projectScoreRepository.findByProjectIdProjectLevel(projectId, 1);
        projectDto.setListLevel1(list1);
        List<ProjectScore> list2 = projectScoreRepository.findByProjectIdProjectLevel(projectId, 2);
        projectDto.setListLevel2(list2);
        List<ProjectScore> list3 = projectScoreRepository.findByProjectIdProjectLevel(projectId, 3);
        projectDto.setListLevel3(list3);
        return projectDto;
    }

    public boolean getIsScoreByProjectId(String projectId) {
        Optional<Project> optionalProject = projectRepository.findById(projectId);
        if (optionalProject.isEmpty()) {
            throw new BadRequestException("项目未找到");
        }
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("登录用户ID未找到");
        }
        Expert expert = expertRepository.findByUserId(userId);
        if (expert == null) {
            throw new BadRequestException("专家未找到");
        }
        List<ProjectScore> list = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expert.getId(), projectId, optionalProject.get().getLevel());
        if (list != null && list.size() == 1) {
            ProjectScore projectScore = list.get(0);
            if (projectScore != null) {
                BigDecimal score = projectScore.getScore();
                if (score != null) {
                    return true;
                }
            }
        }
        return false;
    }

    public ProjectDto getByProjectIdAndLoginUser(String projectId) {
        ProjectDto projectDto = projectService.findById(projectId);
        if (projectDto == null) {
            throw new BadRequestException("项目未找到");
        }
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("登录用户ID未找到");
        }
        Expert expert = expertRepository.findByUserId(userId);
        if (expert == null) {
            throw new BadRequestException("专家未找到");
        }
        List<ProjectScore> list = projectScoreRepository.findByExpertIdAndProjectId(expert.getId(), projectId);
        projectDto.setExpertScoreSituation(list);
        return projectDto;
    }

    public ProjectDto getByProjectIdAndLoginUserSiftLevel(String projectId) {
        ProjectDto projectDto = projectService.findById(projectId);
        if (projectDto == null) {
            throw new BadRequestException("项目未找到");
        }
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            throw new BadRequestException("登录用户ID未找到");
        }
        Expert expert = expertRepository.findByUserId(userId);
        if (expert == null) {
            throw new BadRequestException("专家未找到");
        }
        List<ProjectScore> list1 = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expert.getId(), projectId, 1);
        projectDto.setListLevel1(list1);
        List<ProjectScore> list2 = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expert.getId(), projectId, 2);
        projectDto.setListLevel2(list2);
        List<ProjectScore> list3 = projectScoreRepository.findByExpertIdAndProjectIdWithLevel(expert.getId(), projectId, 3);
        projectDto.setListLevel3(list3);
        return projectDto;
    }
}